﻿#include "systemsetting_1.h"
#include "ui_systemsetting_1.h"

SystemSetting_1::SystemSetting_1(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SystemSetting_1)
{
    ui->setupUi(this);

    ui->labelSystem1Hint->hide();
    ui->frameEnKeyBord->hide();

    time = new QLabel(ui->frameBottom);
    time->setGeometry(10, 10, 200, 40);
    time->setStyleSheet("QLabel{font: 20px; color: rgb(25, 25, 112);}");
    signal = new QLabel(ui->frameBottom);
    signal->setGeometry(700, 18, 41, 22);
    signal->setStyleSheet("QLabel{background-color:transparent;}");

    QDir dir(IMAGE_PATH);
    if(dir.exists())
    {
        ui->frameTop->setStyleSheet(LOGO_STY);
    }

    QRegExp dateTimeRegExp("([1-9])\\d{3}-((0[1-9])|(1[0-2]))\\-((0[1-9])|([1-2]\\d)|(3[0-1]))\\s((0[1-9])|(1\\d)|(2[0-3])):((0\\d)|([1-5]\\d)):((0\\d)|([1-5]\\d))");
    QValidator* validatorDateTime = new QRegExpValidator(dateTimeRegExp, 0);
    ui->m_lineEditYear->setValidator(validatorDateTime);

    QRegExp regExpPileCode("^\\w{16}$");
    QValidator* validatorPileCode = new QRegExpValidator(regExpPileCode, 0);
    ui->m_editPileCode->setValidator(validatorPileCode);

    dateTimeModel = new QStandardItemModel(this);
    dateTimeModel->setColumnCount(1);
    dateTimeModel->setRowCount(1);
    mapper = new QDataWidgetMapper(this);
    mapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);//每当小部件失去焦点时，就将小部件的当前值设置为item模型。
    QStandardItem* root = dateTimeModel->invisibleRootItem();
    m_year = new QStandardItem(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
    root->setChild(0, 0, m_year);
    mapper->setModel(dateTimeModel);
    mapper->addMapping(ui->m_lineEditYear, 0);
    mapper->toFirst();


    devinfoModel = new QStandardItemModel(this);
    devinfoModel->setColumnCount(2);
    devinfoModel->setRowCount(1);
    devmapper = new QDataWidgetMapper(this);
    devmapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
    devroot = devinfoModel->invisibleRootItem();
    devNO = new QStandardItem(readFile(QString(CHARGE_CONF), QString("devNO")));
    propertyCode = new QStandardItem(readFile(QString(CHARGE_CONF), QString("propertyCode")));
    devroot->setChild(0,0,devNO);
    devroot->setChild(0,1,propertyCode);
    devmapper->setModel(devinfoModel);
    devmapper->addMapping(ui->m_editPileCode, 0);
    devmapper->addMapping(ui->m_editPropertyCode,1);
    devmapper->toFirst();
    catchmouse_ = new QTimer(this);
    connect(catchmouse_, SIGNAL(timeout()), this, SLOT(catchpos_()));

    show_date = new QTimer(this);
    connect(show_date,SIGNAL(timeout()),this,SLOT(show_date_fun()));
    show_date->start(1000);
    keyBoardConnect();
    ENkey_board_released();
    toggle = Lower;
}
void SystemSetting_1::show_date_fun()
{
    if(!ui->m_lineEditYear->hasFocus())
    {
        index = dateTimeModel->index(0, 0);
        dateTimeModel->setData(index, QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"), Qt::EditRole);
    }
    if(!ui->m_editPileCode->hasFocus() && !ui->m_editPropertyCode->hasFocus())
    {
        index = devinfoModel->index(0, 0);
        devinfoModel->setData(index, readFile(QString(CHARGE_CONF), QString("devNO")), Qt::EditRole);
        index = devinfoModel->index(0, 1);
        devinfoModel->setData(index, readFile(QString(CHARGE_CONF), QString("propertyCode")), Qt::EditRole);

    }
}
void SystemSetting_1::on_buttonSave_clicked()
{
    /*待完成*/
    if( (!ui->m_lineEditYear->text().isEmpty())  )
    {
        QString date = ui->m_lineEditYear->text();
        QString cmd = "date -s  ";
        system((cmd + "\"" + date + "\"").toLocal8Bit().constData());
        system("hwclock -w");
    }

    //设备号
    bool ret1 = false;
    ret1 = writeFile(QString(CHARGE_CONF), QString("devNO"), ui->m_editPileCode->text());
    if(ret1)
    {
       chargingData->cmd1.flag_change = 0;
    }

    //桩编码
    bool ret0 = false;
    ret0 = writeFile(QString(CHARGE_CONF), QString("propertyCode"), ui->m_editPropertyCode->text());
    if(ret0)
    {
       chargingData->cmd1.flag_change = 0;
    }

    if( !ui->m_editNewPassword->text().isEmpty() && !ui->m_editConfirmPassword->text().isEmpty() ) //设置密码
    {
        if( ui->m_editNewPassword->text() == ui->m_editConfirmPassword->text() )
        {
            MyPasswordFile passWordFile(ui->m_editNewPassword->text());
            passWordFile.store();
        }
        else
        {
            int _ret = QMessageBox::warning(this, tr("提示"), tr("两次输入的密码不一样,请重新输入!!!"), QMessageBox::Yes);

            if(_ret == QMessageBox::Yes)
            {
                ui->m_editNewPassword->setFocus();
                ui->m_editNewPassword->clear();
                ui->m_editConfirmPassword->clear();
                return;
            }
        }
    }
    if(ret0 && ret1)
    {
        ui->labelSystem1Hint->setText(tr("设置成功"));
        ui->labelSystem1Hint->show();
        QTimer::singleShot(3000,ui->labelSystem1Hint,SLOT(hide()));//显示三秒后隐藏
    }
    else
    {
        ui->labelSystem1Hint->setText(tr("设置失败"));
        ui->labelSystem1Hint->show();
        QTimer::singleShot(3000,ui->labelSystem1Hint,SLOT(hide()));//显示三秒后隐藏
    }
}

void SystemSetting_1::on_buttonNext_clicked()
{
    /*待完成*/
    stackLayout->setCurrentIndex(SystemSetting2Page);
}

void SystemSetting_1::on_buttonBack_clicked()
{
    /*待完成*/
    stackLayout->setCurrentIndex(ChooseGunPage);
}

void SystemSetting_1::on_Keyboard_clicked()
{
    QPushButton* btn =  static_cast<QPushButton*>(sender());

    QLineEdit* focus = (QLineEdit* )QApplication::focusWidget();

    keyHandle(btn, focus);

}

void SystemSetting_1::keyHandle(QPushButton* btn, QLineEdit* lineEdit)
{
    if( btn->text() == tr("空格") )
    {
        lineEdit->insert(" ");
    }
    else if( btn->text() == tr("退格") )
    {
        lineEdit->backspace();
    }
    else if( btn->text() == tr("清除"))
    {
        lineEdit->clear();
    }
    else
    {
        lineEdit->insert(btn->text());
    }
}

void SystemSetting_1::keyBoardConnect()
{
    connect(ui->One, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Tow, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Three, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Four, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Five, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Six, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Seven, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Eight, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Nine, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Zero, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Remove, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->RemoveAll, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Char1, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Char2, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Space, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
}

SystemSetting_1::~SystemSetting_1()
{
    delete ui;
}


void SystemSetting_1::on_buttonBoardswitch_clicked()
{
    if(ui->frameEnKeyBord->isHidden())
    {
        ui->frameEnKeyBord->show();
        ui->frameKeyBord->hide();
    }
    else
    {
        ui->frameKeyBord->show();
        ui->frameEnKeyBord->hide();
    }

}
void SystemSetting_1::ENkey_board_released()
{
    connect(ui->OneEN, SIGNAL(pressed()), this, SLOT(EN_pressed_slot()));
    connect(ui->TowEN, SIGNAL(pressed()), this, SLOT(EN_pressed_slot()));
    connect(ui->ThreeEN, SIGNAL(pressed()), this, SLOT(EN_pressed_slot()));
    connect(ui->FourEN, SIGNAL(pressed()), this, SLOT(EN_pressed_slot()));
    connect(ui->FiveEN, SIGNAL(pressed()), this, SLOT(EN_pressed_slot()));
    connect(ui->SixEN, SIGNAL(pressed()), this, SLOT(EN_pressed_slot()));
    connect(ui->SevenEN, SIGNAL(pressed()), this, SLOT(EN_pressed_slot()));
    connect(ui->EightEN, SIGNAL(pressed()), this, SLOT(EN_pressed_slot()));
    connect(ui->NineEN, SIGNAL(pressed()), this, SLOT(EN_pressed_slot()));
    connect(ui->ZeroEN, SIGNAL(pressed()), this, SLOT(EN_pressed_slot()));

    connect(ui->OneEN, SIGNAL(released()), this, SLOT(EN_released_slot()));
    connect(ui->TowEN, SIGNAL(released()), this, SLOT(EN_released_slot()));
    connect(ui->ThreeEN, SIGNAL(released()), this, SLOT(EN_released_slot()));
    connect(ui->FourEN, SIGNAL(released()), this, SLOT(EN_released_slot()));
    connect(ui->FiveEN, SIGNAL(released()), this, SLOT(EN_released_slot()));
    connect(ui->SixEN, SIGNAL(released()), this, SLOT(EN_released_slot()));
    connect(ui->SevenEN, SIGNAL(released()), this, SLOT(EN_released_slot()));
    connect(ui->EightEN, SIGNAL(released()), this, SLOT(EN_released_slot()));
    connect(ui->NineEN, SIGNAL(released()), this, SLOT(EN_released_slot()));
    connect(ui->ZeroEN, SIGNAL(released()), this, SLOT(EN_released_slot()));
}
void SystemSetting_1::EN_pressed_slot()
{
    btnEN =  static_cast<QPushButton*>(sender());
    released_text = btnEN->text().remove("\n");
    qDebug()<<"++++++++++++++++++++++++"<<released_text;
    catchmouse_->start(50);
}
void SystemSetting_1::EN_released_slot()
{
    catchmouse_->stop();
    QLineEdit* focus = (QLineEdit* )QApplication::focusWidget();
    focus->insert(ui->ShowEN->text());
    ui->ShowEN->clear();
}
void SystemSetting_1::catchpos_()
{
    coursePoint = btnEN->mapFromGlobal(QCursor::pos());
    if(released_text.length() == 3)
    {
        switch(coursePoint.x()/20)
        {
        case 0:
            ui->ShowEN->setText(released_text.at(0));
            break;
        case 1:
            ui->ShowEN->setText(released_text.at(1));
            break;
        case 2:
            ui->ShowEN->setText(released_text.at(2));
            break;
        }
    }
    else if(released_text.length() == 4)
    {
        switch(coursePoint.x()/15)
        {
        case 0:
            ui->ShowEN->setText(released_text.at(0));
            break;
        case 1:
            ui->ShowEN->setText(released_text.at(1));
            break;
        case 2:
            ui->ShowEN->setText(released_text.at(2));
            break;
        case 3:
            ui->ShowEN->setText(released_text.at(3));
            break;
        }
    }
}
void SystemSetting_1::on_Char3_2_clicked()
{
    if(toggle == Lower)
    {
        ui->TowEN->setText(ui->TowEN->text().toUpper());
        ui->ThreeEN->setText(ui->ThreeEN->text().toUpper());
        ui->FourEN->setText(ui->FourEN->text().toUpper());
        ui->FiveEN->setText(ui->FiveEN->text().toUpper());
        ui->SixEN->setText(ui->SixEN->text().toUpper());
        ui->SevenEN->setText(ui->SevenEN->text().toUpper());
        ui->EightEN->setText(ui->EightEN->text().toUpper());
        ui->NineEN->setText(ui->NineEN->text().toUpper());
        ui->ZeroEN->setText(ui->ZeroEN->text().toUpper());
        toggle = Upper;
    }
    else
    {
        ui->TowEN->setText(ui->TowEN->text().toLower());
        ui->ThreeEN->setText(ui->ThreeEN->text().toLower());
        ui->FourEN->setText(ui->FourEN->text().toLower());
        ui->FiveEN->setText(ui->FiveEN->text().toLower());
        ui->SixEN->setText(ui->SixEN->text().toLower());
        ui->SevenEN->setText(ui->SevenEN->text().toLower());
        ui->EightEN->setText(ui->EightEN->text().toLower());
        ui->NineEN->setText(ui->NineEN->text().toLower());
        ui->ZeroEN->setText(ui->ZeroEN->text().toLower());
        toggle = Lower;
    }
}

void SystemSetting_1::on_Char1_2_clicked()
{
    QLineEdit* focus = (QLineEdit* )QApplication::focusWidget();
    focus->insert(ui->Char1_2->text());
}

void SystemSetting_1::on_Char2_2_clicked()
{
    QLineEdit* focus = (QLineEdit* )QApplication::focusWidget();
    focus->insert(ui->Char2_2->text());
}

void SystemSetting_1::on_RemoveAll_2_clicked()
{
    QLineEdit* focus = (QLineEdit* )QApplication::focusWidget();
    focus->clear();
}

void SystemSetting_1::on_Remove_2_clicked()
{
    QLineEdit* focus = (QLineEdit* )QApplication::focusWidget();
    focus->backspace();
}
